angular.module('websem', ['ui.bootstrap', 'ngRoute'])

.run(function($rootScope) {
    $rootScope.requetes = [
		{requete:"personnes Intouchables",                       value:1},
		{requete:"\"lieu naissance\" \"Omar Sy\"",               value:2},
		{requete:"personnes recompensées Intouchables",          value:3},
		{requete:"palmarès \"Globes de Cristal 2012\"",          value:4},
		{requete:"\"membre jury\" \"Globes de Cristal 2012\"",   value:5},
		{requete:"prix \"Omar Sy\" \"Globes de Cristal 2012\"",  value:6},
		{requete:"lieu \"Globes Cristal 2012\"",                 value:7},
		{requete:"prix \"Omar Sy\"",                             value:8},
		{requete:"acteurs \"joué avec\" \"Omar Sy\"" ,           value:9}
	];
})

.controller('WebsemController',
    [
        '$scope', '$http', '$rootScope',
        function($scope, $http, $rootScope) {
			
			$scope.requetes = $rootScope.requetes;
			
			$scope.search = function(word) {
				$http.post("php/search.php", {
					query:word
				}).success(function (data) {
					$scope.query = data.query;
					$scope.liste = data.documents;
					$scope.openAccordion(word);
				})
				.error(function (error) {
					$scope.error = error;
				});
            };
			$scope.remplir = function(word) {
				var regex = new RegExp(", *", "gi");
				word = word.replace(regex, " ");
				$scope.word = word;
				$scope.search($scope.word);
            };
			
			$scope.openAccordion = function(word){
				var regex = new RegExp(" *", "gi");
				var id = word.replace(regex, "_");
				//$("#" + id).attr("is-open", true);
				//$("#" + id)
			}
        }
    ]
)

.controller('AccordionCtrl',
    [
        '$scope', '$http', '$rootScope',
        function($scope, $http, $rootScope) {
		
			$scope.open = true;
			
			$scope.getStatistics = function() {
				$http.post("php/statistiques.php").success(function (data) {
				
					$scope.statistiques = data.statistiques;				
					
					$scope.moyennePrecision = data.moyennes.moyennePrecision;
					$scope.moyennePrecision5 = data.moyennes.moyennePrecision5;
					$scope.moyennePrecision10 = data.moyennes.moyennePrecision10;
					$scope.moyennePrecision25 = data.moyennes.moyennePrecision25;
					
					$scope.moyenneRappel = data.moyennes.moyenneRappel;
					$scope.moyenneRappel5 = data.moyennes.moyenneRappel5;
					$scope.moyenneRappel10 = data.moyennes.moyenneRappel10;
					$scope.moyenneRappel25 = data.moyennes.moyenneRappel25;
				})
				.error(function (error) {
					$scope.error = error;
				});
            };
			
			$scope.getStatistics();
		}
	]
);